package com.gl.goods.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public class BaseListAdapter<T> extends BaseAdapter {

    private LayoutInflater mInflater;
    protected Context mContext;

    public BaseListAdapter(Context context) {
        this.mContext = context;
    }

    protected LayoutInflater getLayoutInflater(Context context) {
        if (mInflater == null) {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        return mInflater;
    }

    protected List<T> mList = new ArrayList<T>();

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public T getItem(int position) {
        T t = null;
        if (mList != null && mList.size() > position) {
            t = mList.get(position);
        }
        return t;
    }

    public List<T> getData() {
        return mList == null ? (mList = new ArrayList<T>()) : mList;
    }

    public void setData(List<T> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void addData(List<T> list) {
        if (mList != null && list != null && !list.isEmpty()) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void addItem(T obj) {
        if (mList != null) {
            mList.add(obj);
        }
        notifyDataSetChanged();
    }

    public void addItem(int pos, T obj) {
        if (mList != null) {
            mList.add(pos, obj);
        }
        notifyDataSetChanged();
    }

    public void removeItem(Object obj) {
        mList.remove(obj);
        notifyDataSetChanged();
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return null;
    }

}
